package com.terrynoya.fight.data.parser.sff
{
	import com.terrynoya.fight.data.parser.sff.Bytes;
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	/**
	 * @author yaojianzhou
	 */
	public class Palette
	{
		private var _colors:Array;
		
		public function Palette(bytes:ByteArray):void
		{
			bytes.endian = Endian.LITTLE_ENDIAN;
			_colors = new Array();
			while(bytes.bytesAvailable > 0)
			{
				var colorBytes:Bytes = new Bytes(4);
				colorBytes.write(bytes);
				this._colors.push(this.bytes2Color(colorBytes.data));
			}
		}

		public function getColor(colorIndex:int,alpha:uint) : uint
		{
			var color16:uint = this._colors[colorIndex];
			return (alpha << 24 ) | color16;
		}
		
		private function bytes2Color(bytes:ByteArray) : uint
		{
			var r:uint = bytes.readUnsignedByte();
			var g:uint = bytes.readUnsignedByte();
			var b:uint = bytes.readUnsignedByte();
			//not used now in sffv2
//			var a:uint = bytes.readUnsignedByte();
			var rlt:uint = (r << 16) | (g << 8) | b;
			return rlt;
		}
	}
}
